package edu.ccut.saturn.basecomponent.database.resultset.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.ccut.saturn.basecomponent.database.resultset.IResultSetSet;
import edu.ccut.saturn.component.SaturnData;

public class ResultSetGetClob implements IResultSetSet {

	public void setSaturnData(SaturnData saturnData, String key,
			ResultSet resultSet) throws SQLException {

		Clob clob = null;
		if ((clob = resultSet.getClob(key)) != null) {

			int length = (int) clob.length();

			InputStream inputStream = clob.getAsciiStream();

			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(inputStream));

			char[] charValue = new char[length];

			try {

				bufferedReader.read(charValue);

			} catch (IOException e) {
				e.printStackTrace();
			} finally {

				// if (bufferedReader != null){
				// try {
				// bufferedReader.close();
				// } catch (IOException e) {
				// e.printStackTrace();
				// }
				// }

				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			String value = new String(charValue);

			saturnData.set(key, value);

		} else {
			saturnData.set(key, clob);
		}
	}
}
